<template>
	<view class="new-users copy-data">
		<view class="bg"></view>
		<view class="head">
			<view class="user-card">
				<view class="bg"></view>
				<view class="user-info">
					<!-- 注释这个是加的bnt -->
					<!-- #ifdef H5 -->
					<button class="bntImg" v-if="userInfo.is_complete == 0 && isWeixin" @click="getWechatuserinfo">
						<image class="avatar" src='/static/images/f.png'></image>
						<view class="avatarName">获取头像</view>
					</button>
					<!-- #endif -->
					<!-- #ifdef MP -->
					<button class="bntImg" v-if="userInfo.is_complete == 0" open-type='getUserInfo' @getuserinfo="getRoutineUserInfo">
						<image class="avatar" src='/static/images/f.png'></image>
						<view class="avatarName">获取头像</view>
					</button>
					<!-- #endif -->
					<view v-else>
						<image class="avatar" :src='userInfo.avatar' v-if="userInfo.avatar" @click="goEdit()"></image>
						<image v-else class="avatar" src="/static/images/f.png" mode="" @click="goEdit()"></image>
					</view>


					<view class="info">
						<!-- #ifdef MP -->
						<view class="name" v-if="!userInfo.uid" @tap="openAuto">
							请点击授权
						</view>
						<!-- #endif -->
						<view class="name" v-if="userInfo.uid">
							{{userInfo.nickname}}
							<view class="vip" v-if="userInfo.vip">
								<image :src="userInfo.vip_icon" alt="">
									<view style="margin-left: 10rpx;" class="vip-txt">{{userInfo.vip_name}}</view>
							</view>
						</view>
						<view class="num" v-if="userInfo.phone">
							<view class="num-txt">ID(邀请码):{{userInfo.self_inv_code}}</view>
							<!-- <view class="icon">
								<image src="/static/images/edit.png" mode=""></image>
							</view> -->
						</view>
            <!-- <view class="agency">武汉市光谷区代理</view> -->
						<view class="phone" v-if="!userInfo.phone && isLogin" @tap="bindPhone">绑定手机号</view>
					</view>
				</view>
				<!-- <view class="sign" @click="goSignIn">签到</view> -->
			</view>
			<view class="iconfont icon-shezhi" @click="goEdit()"></view>
		</view>
		<view class="main">
			<view class="num-wrapper">
				<view class="num-item" v-if="userInfo.balance_func_status" @click="goMenuPage('/pages/users/user_money/index')">
					<view class="txt">余额</view>
					<text class="num">{{userInfo.now_money || 0}}</text>
				</view>
        <view class="num-item">
					<view class="txt">发团点</view>
					<text class="num">{{userInfo.start_num || 0}}</text>
				</view>
				<view class="num-item">
					<view class="txt">积分</view>
					<text class="num">{{userInfo.points || 0}}</text>
				</view>
			</view>
      <view class="tuan-wrapper">
        <view class="tuan-box" @click="getPinklist(1)">
          <image src="/static/images/fatuan.png" class="tuan-img"> </image>
				  <text>发团订单</text>
        </view>
        <text class="tcr">|</text>
        <view class="tuan-box"  @click="getPinklist(2)">
            <image src="/static/images/canyu.png" class="tuan-img"></image>
            <text>参与订单</text>
          </view>
      </view>
			<view class="order-wrapper" style="display:none">
				<view class="order-hd flex">
					<view class="left">我的订单</view>
					<navigator class="right flex" hover-class="none" url="/pages/users/order_list/index1" open-type="navigate">
						查看全部订单
						<text class="iconfont icon-xiangyou"></text>
					</navigator>
				</view>
				<view class="order-bd">
					<block v-for="(item,index) in orderMenu" :key="index">
						<navigator class="order-item" open-type="navigate" hover-class="none" :url="item.url">
							<view class="pic">
								<image :src="item.img" mode=""></image>
								<text class="order-status-num" v-if="item.num > 0">{{ item.num }}</text>
							</view>
							<view class="txt">{{item.title}}</view>
						</navigator>
					</block>
				</view>
			</view>
      <view class="list-wrapper" >
         <block v-for="(item,index) in listMenu" :key="index">
          <navigator hover-class="none" :url="item.url">
            <view class="list-bd">
              <view>
                <image :src="item.img" class="tuan-img"></image>
                <text>{{item.title}}</text>
              </view>
              <view class="flex"><text class="iconfont icon-xiangyou"></text></view>
            </view>
          </navigator>
         </block>
      </view>
		</view>
		<!-- <recommend :hostProduct="hostProduct"></recommend> -->
		<!-- <img src="/static/images/support.png" alt="" class='support'> -->
		<view style="height: 50rpx;"></view>
		<!-- #ifdef MP -->
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
		<!-- #endif -->
	</view>
</template>
<script>
	import {
		getProductHot
	} from '@/api/store.js';
	import recommend from '@/components/recommend/index';
	import {
		getMenuList,
		getUserInfo,
		setVisit,
		updateUserInfo
	} from '@/api/user.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	// #ifdef H5
	import Auth from '@/libs/wechat';
	// #endif
	const app = getApp();
	export default {
		components: {
			recommend,
			// #ifdef MP
			authorize
			// #endif
		},
		computed: mapGetters(['isLogin']),
		data() {
			return {
				orderMenu: [{
						img: '/static/images/order1.png',
						title: '待付款',
						url: '/pages/users/order_list/index1?status=0'
					},
					{
						img: '/static/images/order2.png',
						title: '待发货',
						url: '/pages/users/order_list/index1?status=1'
					},
					{
						img: '/static/images/order3.png',
						title: '待收货',
						url: '/pages/users/order_list/index1?status=2'
					},
					{
						img: '/static/images/order4.png',
						title: '待评价',
						url: '/pages/users/order_list/index1?status=3'
					},
					{
						img: '/static/images/order5.png',
						title: '售后/退款',
						url: '/pages/users/user_return_list/index'
					},
        ],
        listMenu: [
        // {
        //   img: '/static/images/shop.png',
        //   title: '购物车',
        //   url: '/pages/users/order_pay_status/index'
        // },
        {
          img: '/static/images/card.png',
          title: '绑定银行卡',
          url: '/pages/users/user_bank_card_edit/index'
        },
        {
          img: '/static/images/ka.png',
          title: '提现',
          url: '/pages/users/user_cash/index'
        },
        {
          img: '/static/images/map.png',
          title: '收货地址',
          url: '/pages/users/user_address_list/index'
        },
        {
          img: '/static/images/tui.png',
          title: '推广中心',
          url: '/pages/users/user_extend/index'
        }],
				imgUrls: [],
				autoplay: true,
				circular: true,
				interval: 3000,
				duration: 500,
				isAuto: true, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				orderStatusNum: {},
				userInfo: {},
        MyMenus: [],
				// #ifdef H5
				isWeixin: Auth.isWeixin(),
				//#endif
				hostProduct: [],
				hotPage: 1,
				hotScroll: 0,
			}
		},
		onLoad() {
      let that = this;
			that.$set(that, 'MyMenus', app.globalData.MyMenus);
			// #ifdef H5 || APP-PLUS
			if (that.isLogin == false) {
				toLogin();
			}
			// #endif
			this.get_host_product()
		},
		onShow: function() {
			let that = this;
			if (that.isLogin) {
				this.getUserInfo();
				this.getMyMenus();
				this.setVisit();
			};
		},
		methods: {
      getPinklist(type) {
        if(type == 1) {
          uni.navigateTo({
             url: '/pages/users/pinkList/index'
          });
        }else {
           uni.navigateTo({
             url: '/pages/users/joinOrder/index'
          });
        };
      },
			getWechatuserinfo() {
        //#ifdef H5 
				Auth.isWeixin() && Auth.oAuth('snsapi_userinfo');
				//#endif
			},
			getRoutineUserInfo(e) {
				updateUserInfo({
					userInfo: e.detail.userInfo
				}).then(res => {
					this.getUserInfo();
					return this.$util.Tips('更新用户信息成功');
				}).catch(res => {

				})
				console.log(e);
			},
			// 记录会员访问
			setVisit() {
				setVisit({
					url: '/pages/user/index'
				}).then(res => {})
			},
			// 打开授权
			openAuto() {
				this.isAuto = true;
				this.isShowAuth = true
			},
			// 授权回调
			onLoadFun() {
				this.getUserInfo();
				this.getMyMenus();
				this.setVisit();
			},
			Setting: function() {
				uni.openSetting({
					success: function(res) {
						console.log(res.authSetting)
					}
				});
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			// 绑定手机
			bindPhone() {
				uni.navigateTo({
					url: '/pages/users/user_phone/index'
				})
			},
			/**
			 * 获取个人用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					console.log('我的信息',res.data);
					that.userInfo = res.data
					that.$store.commit("SETUID", res.data.uid);
					that.orderMenu.forEach((item, index) => {
						switch (item.title) {
							case '待付款':
								item.num = res.data.orderStatusNum.unpaid_count
								break
							case '待发货':
								item.num = res.data.orderStatusNum.unshipped_count
								break
							case '待收货':
								item.num = res.data.orderStatusNum.received_count
								break
							case '待评价':
								item.num = res.data.orderStatusNum.evaluated_count
								break
							case '售后/退款':
								item.num = res.data.orderStatusNum.refund_count
								break
						}
					})
				});
			},
			/**
			 * 
			 * 获取个人中心图标
			 */
			getMyMenus: function() {
				let that = this;
				if (this.MyMenus.length) return;
				getMenuList().then(res => {
					that.$set(that, 'MyMenus', res.data.routine_my_menus);
					this.imgUrls = res.data.routine_my_banner
				});
			},
			// 编辑页面
			goEdit() {
				uni.navigateTo({
					url: '/pages/users/user_info/index'
				})
			},
			// 签到
			goSignIn() {
				uni.navigateTo({
					url: '/pages/users/user_sgin/index'
				})
			},
			// goMenuPage
			goMenuPage(url) {
				if (this.isLogin) {
					uni.navigateTo({
						url
					})
				} else {
					// #ifdef MP
					this.openAuto()
					// #endif
				}
			},
			get_host_product: function() {
				let that = this;
				if(that.hotScroll) return
				getProductHot(
					that.hotPage,
					that.hotLimit,
				).then(res => {
					that.hotPage++
					that.hotScroll = res.data.length<that.hotLimit
					that.hostProduct = that.hostProduct.concat(res.data)
				});
			}
		},
		onReachBottom() {
			this.get_host_product();
		}
	}
</script>
<style lang="scss">
	.bg{
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: -1;
		background: #f5f5f5;
	}
	.new-users {
		.head {
			.user-card {
				position: relative;
				width: 100%;
				height: 190rpx;
				margin: 0 auto;
				padding: 35rpx 28rpx;
				background:#EF271E;

				&::after {
					position: absolute;
					left: 0;
					right: 0;
					bottom: -98rpx;
					content: '';
					height: 100rpx;
					width: 100%;
					border-radius: 0 0 50% 50%;
					background-color: #EF271E;
				}

				.user-info {
					z-index: 20;
					position: relative;
					display: flex;

					.bntImg {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						text-align: center;
						line-height: 120rpx;
						background-color: unset;
						position: relative;

						.avatarName {
							font-size: 16rpx;
							color: #fff;
							text-align: center;
							background-color: rgba(0, 0, 0, 0.6);
							height: 37rpx;
							line-height: 37rpx;
							position: absolute;
							bottom: 0;
							left: 0;
							width: 100%;
						}
					}

					.avatar {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
					}

					.info {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						margin-left: 20rpx;
						padding: 15rpx 0;

						.name {
							display: flex;
							align-items: center;
							color: #fff;
							font-size: 31rpx;

							.vip {
								display: flex;
								align-items: center;
								height: 36rpx;
								padding: 0 20rpx;
								background: rgba(0, 0, 0, 0.2);
								border-radius: 18px;
								font-size: 20rpx;
								margin-left: 12rpx;

								image {
									width: 27rpx;
									height: 27rpx;
								}
							}
						}

						.num {
							display: flex;
							align-items: center;
							font-size: 26rpx;
							color:#fff;
              margin: 10rpx 0;
							image {
								width: 22rpx;
								height: 23rpx;
								margin-left: 20rpx;
							}
            }
            .agency {
              font-size: 24rpx;
              color: #fff;
            }
					}
				}



				.sign {
					z-index: 200;
					position: absolute;
					right: -12rpx;
					top: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 120rpx;
					height: 60rpx;
					background: linear-gradient(90deg, rgba(255, 225, 87, 1) 0%, rgba(238, 193, 15, 1) 100%);
					border-radius: 29rpx 4rpx 4rpx 29rpx;
					color: #282828;
					font-size: 28rpx;
					font-weight: bold;
				}
			}
		}

		.main {
			position: relative;
			top: -48rpx;
			padding: 0 20rpx;

			.num-wrapper {
				z-index: 30;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 60rpx;
				height: 140rpx;
				border-radius: 6rpx;
				// padding: 0 47rpx;
				color: #333;
				background: #fff;

				.num-item {
					width: 33.33%;

					text-align: center;

					.num {
						font-size: 32rpx;
					}

					.txt {
						margin-top: 8rpx;
						font-size: 26rpx;
						color: #aaa;
					}
				}
			}
      .tuan-wrapper {
        z-index: 30;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;
        height: 140rpx;
				border-radius: 6rpx;
				// padding: 0 47rpx;
				color: #333;
        background: #fff;
        .tcr {
          color: #ccc;
        }
        .tuan-box {
          width: 50%;
          text-align: center;
          font-size: 32rpx;
          color: #333333;
          .tuan-img {
          width: 44rpx;
          height: 48rpx;
          vertical-align: middle;
          margin-right: 14rpx;
        }
        }
        
      }
			.order-wrapper {
				background-color: #fff;
        border-radius: 6rpx;
        padding: 0 10rpx;
				.order-hd {
					justify-content: space-between;
					padding:20rpx 28rpx;
					margin-top: 20rpx;
					border-bottom: 1px solid #F5F5F5;
					font-size: 30rpx;
					color: #282828;

					.right {
						align-items: center;
						color: #666666;
						font-size: 26rpx;

						.icon-xiangyou {
							margin-left: 5rpx;
							margin-top: 6rpx;
							font-size: 26rpx;
						}
					}
				}

				.order-bd {
					display: flex;

					.order-item {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 20%;
						height: 160rpx;

						.pic {
							position: relative;
							text-align: center;

							image {
								width: 49rpx;
								height: 42rpx;
							}
						}

						.txt {
							margin-top: 15rpx;
							font-size: 26rpx;
							color: #454545;
						}
					}
				}
      }
      .list-wrapper {
        background-color: #fff;
        border-radius: 6rpx;
        padding: 0 10rpx;
        margin-top: 20rpx;
        .list-bd {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding:20rpx 28rpx;
          border-bottom: 1px solid #F5F5F5;
          font-size: 32rpx;
          .tuan-img {
            width: 44rpx;
            height: 44rpx;
            vertical-align: middle;
            margin-right: 14rpx;
          }
        }
      }
		}

		.slider-wrapper {
			margin: 20rpx 0;
			height: 130rpx;

			swiper,
			swiper-item {
				height: 100%;
			}

			image {
				width: 100%;
				height: 130rpx;
			}
		}
		.user-menus {
			background-color: #fff;
			display: flex;
			flex-wrap: wrap;
			.item {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				width: 25%;	
				height: 120rpx;
				font-size: 24rpx;
				image {
					width: 46rpx;
					height: 46rpx;
					margin-bottom: 10rpx;
				}
				
			}

			button {
				font-size: 28rpx;
			}
		}

		.phone {
			color: #fff;
		}

		.order-status-num {

			min-width: 12rpx;
			background-color: #fff;
			color: #ee5a52;
			border-radius: 15px;
			position: absolute;
			right: -14rpx;
			top: -15rpx;
			font-size: 20rpx;
			padding: 0 8rpx;
			border: 1px solid #ee5a52;
		}

		.support {
			width: 219rpx;
			height: 74rpx;
			margin: 54rpx auto;
			display: block;
		}
	}
	.title-box{
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 30rpx;
		border-bottom: 1px dashed #eee;
		background-color: #fff;
	}
	.icon-shezhi{
		z-index: 99;
		color: #fff;
		font-size: 40rpx;
		position: absolute;
		right: 22rpx;
		top: 60rpx;
	}
</style>
